package dk.hewison.util;


import java.io.IOException;

/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 16:53:28 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 348 $:
 */
public class Exif {
/*
    *//**
     * Creates a thumbnail of the given file. The thumbnails largest dimension will
     * be size. Throws an exception if the image cannot be read.
     *
     * @param file File The image to be read.
     * @param size int Length of the longest dimension of the thumbnail.
     * @return BufferedImage
     * @throws IOException If image cannot be read.
     *//*
    public static void makeThumbnailFile(File file, String outputFilename, int size) throws IOException {
        BufferedImage image = null;
        // currently metadata extractor only works for JPG images
        if (file.getName().toUpperCase().endsWith("JPG") ||
                file.getName().toUpperCase().endsWith("JPEG")) {
            try {
                // locate the bytes of the thumbnail data and read them using ImageIO
                // (thumbnail data will usually be a JPG image itself, if not there is a fallback)
                Metadata metadata = JpegMetadataReader.readMetadata(file);
                if (metadata.containsDirectory(ExifDirectory.class)) {
                    Directory dir = metadata.getDirectory(ExifDirectory.class);
                    if (dir.containsTag(ExifDirectory.TAG_THUMBNAIL_DATA)) {
                        byte[] bytes = dir.getByteArray(ExifDirectory.TAG_THUMBNAIL_DATA);
                        image = ImageIO.read(new ByteArrayInputStream(bytes));
                    }
                }
            } catch (JpegProcessingException ex) {
                ex.printStackTrace();
            } catch (MetadataException ex) {
                ex.printStackTrace();
            }
        }

        // if we failed to read the thumbnail we read the entire file
        if (image == null) {
            Log.debug("had to read the whole image");
            image = ImageIO.read(file);
        }
        // now we just have to scale the image to the desired size
        // no rendering hints used... it's just a thumbnail anyway
        *//* double scaleX = size / (double) image.getWidth();
double scaleY = size / (double) image.getHeight();
double scale = Math.min(scaleX, scaleY);
final BufferedImage thumb = new BufferedImage(
        (int) Math.round(scale * image.getWidth()),
        (int) Math.round(scale * image.getHeight()),
        BufferedImage.TYPE_4BYTE_ABGR);*//*
        *//*Graphics g = thumb.createGraphics();
        g.drawImage(image, 0, 0, thumb.getWidth(), thumb.getHeight(), null);
        g.dispose();*//*

        //return thumb;
        ImageIO.write(image, "JPG", new File(outputFilename));
    }

    public static void main(String[] args) {

        try {

            long start = System.currentTimeMillis();
            //String[] fileNames = {"PIA03231.JPG", "PIA03241.JPG", "PIA08814.JPG", "PIA09201.JPG", "PIA09202.JPG", "PIA09203.JPG", "PIA09205.JPG", "PIA09207.JPG", "PIA09259.JPG", "large4.png", "large3.png", "large2.png", "large.png", "marbles.tif", "xing_t24.tif"};

            //File file = new File("/picture/UK2008/");
            String a = "/Users/johnhewison/Pictures/UK2008/P1000001.JPG";
            String aout = "/Users/johnhewison/Pictures/UK2008/P1000001a.JPG";

            String b = "/Users/johnhewison/Pictures/UK2008/P1000002.JPG";
            String bout = "/Users/johnhewison/Pictures/UK2008/P1000002a.JPG";
            //854 - 3145

            String c = "/Users/johnhewison/Pictures/UK2008/P1000003.JPG";
            String cout = "/Users/johnhewison/Pictures/UK2008/P1000003a.JPG";
            //828

            String d = "/Users/johnhewison/Pictures/UK2008/P1000005.JPG";
            String dout = "/Users/johnhewison/Pictures/UK2008/P1000005a.JPG";
            //900

            String e = "/Users/johnhewison/Pictures/UK2008/P1000006.JPG";
            String eout = "/Users/johnhewison/Pictures/UK2008/P1000006a.JPG";
            //945

            makeThumbnailFile(new File(a), aout, 144);
            makeThumbnailFile(new File(b), bout, 144);
            makeThumbnailFile(new File(c), cout, 144);
            makeThumbnailFile(new File(d), dout, 144);
            makeThumbnailFile(new File(e), eout, 144);

            long stop = System.currentTimeMillis();
            Log.debug("Done. " + (stop - start));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }*/
}
